package com.wc.alorithm_blue_bridge_history.littlewhite.Q240406.E_无理数位数查询;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/6 20:22
 * @description https://www.lanqiao.cn/problems/17161/learning/?contest_id=179
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int t, m;
    static long n;
    static int M = 15, N = 110;
    static long INF = (long) 1e18 - 10;
    static long[][] f = new long[M][N];

    public static void main(String[] args) {
        t = sc.nextInt();
        for (int i = 2; i < M; i++) {
            for (int j = 1; ; j++) {
                if (j == 1) f[i][j] = i - 1;
                else f[i][j] = f[i][j - 1] * i;
                if (f[i][j] > INF) {
                    f[i][0] = j;
                    break;
                }
            }
        }
        for (int i = 2; i < M; i++) {
            for (int j = 2; j <= f[i][0]; j++) {
                f[i][j] = f[i][j] * j + f[i][j - 1];
                if (f[i][j] > INF) {
                    f[i][0] = j;
                    break;
                }
            }
        }
        while (t-- > 0) {
            n = sc.nextLong();
            m = sc.nextInt();
            int l = 0, r = (int) f[m][0];
            int dig = 0;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if (f[m][mid] < n) l = mid;
                else r = mid - 1;

            }
            dig = r + 1;
            // 剩余的都是r位数
            if (dig > 1)
                n -= f[m][dig - 1];
            // r位第几个
            long rand = (n - 1) / dig;
            long first = (long) Math.pow(m, dig - 1);
            long now = first + rand;
            int k = (int) (n - 1 - rand * dig);
            String s = Long.toString(now, m);
            out.println(s.charAt(k));
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
